﻿using System;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace SmartHealthCare.Utils
{
    internal static class Util
    {
        /// <summary>
        /// Reads the only.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void ReadOnly(Control control, bool value)
        {
            foreach (Control c in control.Controls)
            {
                if (c.HasChildren)
                    ReadOnly(c, value);
                if (c is TextBoxBase)
                {
                    ((TextBoxBase)c).ReadOnly = value;
                    c.BackColor = Color.White;
                }

            }
        }
        /// <summary>
        /// Limppiars the specified control.
        /// </summary>
        /// <param name="control">The control.</param>
        public static void Limpiar(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c.HasChildren)
                    Limpiar(c);
                if (c is TextBox || c is MaskedTextBox)
                    c.Text = string.Empty;
            }
        }

        /// <summary>
        /// Yellows the color.
        /// </summary>
        /// <param name="control">The control.</param>
        public static void YellowColor(Control control)
        {
            foreach (Control item in control.Controls)
            {
                if (item.HasChildren)
                    YellowColor(item);
                //
                if (item is TextBoxBase)
                    item.BackColor = Color.LightYellow;
            }
        }
        /// <summary>
        /// Blues the color.
        /// CornflowerBlue
        /// </summary>
        /// <param name="control">The control.</param>
        public static void BlueColor(Control control)
        {
            foreach (Control item in control.Controls)
            {
                if (item.HasChildren)
                    BlueColor(item);
                //
                if (item is TextBoxBase)
                    item.BackColor = Color.CornflowerBlue;
            }
        }



        /// <summary>
        /// Serializes the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        public static void Serialize(object obj)
        {
            var savefile = new SaveFileDialog
                               {
                                   OverwritePrompt = true,
                                   AutoUpgradeEnabled = true,
                                   AddExtension = true,
                                   DefaultExt = ".shc",
                                   InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                                   Filter = @"Smart Heath Care File (*.shc)|*.txt|All files (*.*)|*.*"
                               };

            if (savefile.ShowDialog() != DialogResult.OK) return;

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(savefile.FileName, FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, obj);
            stream.Close();
            stream.Dispose();
        }

        /// <summary>
        /// Des the serialise.
        /// </summary>
        /// <returns></returns>
        public static object DeSerialise()
        {
            object obj = null;
            var openfile = new OpenFileDialog
                               {
                                   AutoUpgradeEnabled = true,
                                   AddExtension = true,
                                   DefaultExt = ".shc",
                                   InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                                   Filter = @"Smart Heath Care File (*.shc)|*.txt|All files (*.*)|*.*"
                               };
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(openfile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                obj = formatter.Deserialize(stream);
                stream.Close();
                stream.Dispose();
            }
            return obj;
        }

    }



}
